<template>
  <n-modal
    v-model:show="showModal"
    :show-icon="false"
    preset="dialog"
    :title="title"
    :style="{ width: props.width + 'px' }"
    @update:show="handleUpdateShow"
  >
    <n-spin :show="modalLoading" content-class="max-h-[80vh] overflow-auto pr-2">
      <n-form :model="formData" :rules="formRules" ref="formRef" label-width="auto" label-placement="top">
        <n-grid :cols="1" :x-gap="24">
          <n-form-item-gi label="接口名称" path="apiNm">
            <n-input placeholder="请输入接口名称" v-model:value="formData.apiNm" />
          </n-form-item-gi>
          <n-form-item-gi label="网络地址" path="path">
            <n-input placeholder="请输入网络地址" v-model:value="formData.path" />
          </n-form-item-gi>
          <n-form-item-gi label="方法" path="methd">
            <n-input placeholder="请输入方法" v-model:value="formData.methd" />
          </n-form-item-gi>
          <n-form-item-gi label="请求总数" path="reqTotl">
            <n-input placeholder="请输入请求总数" v-model:value="formData.reqTotl" />
          </n-form-item-gi>
          <n-form-item-gi label="请求最大耗时" path="reqMaxDur">
            <n-input placeholder="请输入请求最大耗时" v-model:value="formData.reqMaxDur" />
          </n-form-item-gi>
          <n-form-item-gi label="分组编号" path="grpId">
            <n-input placeholder="请输入分组编号" v-model:value="formData.grpId" />
          </n-form-item-gi>
          <n-form-item-gi label="应用代码" path="appCd">
            <n-input placeholder="请输入应用代码" v-model:value="formData.appCd" />
          </n-form-item-gi>
          <n-form-item-gi label="渠道编号" path="chnlId">
            <n-input placeholder="请输入渠道编号" v-model:value="formData.chnlId" />
          </n-form-item-gi>
          <n-form-item-gi label="系统编号" path="sysId">
            <n-input placeholder="请输入系统编号" v-model:value="formData.sysId" />
          </n-form-item-gi>
          <n-form-item-gi label="备注" path="rmrk">
            <n-input placeholder="请输入备注" v-model:value="formData.rmrk" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" />
          </n-form-item-gi>
        </n-grid>
      </n-form>
    </n-spin>
    <template #action>
      <n-space>
        <n-button @click="() => handleUpdateShow(false)">取消</n-button>
        <n-button v-if="hasPermission('bmpch:web:api:save', '保存Wep接口')" type="info" :loading="saveBtnLoading" @click="handleSave">
          <template #icon>
            <n-icon>
              <SaveOutlined />
            </n-icon>
          </template>
          保存
        </n-button>
      </n-space>
    </template>
  </n-modal>
</template>

<script lang="ts" setup>
import { reactive, ref, unref, watch } from 'vue';
import type { FormRules, FormInst } from 'naive-ui';
import { SaveOutlined } from '@vicons/antd';
import type * as BMPCH from '@yanfan/bmpch-tsapi/types';
import { webApiGet, webApiSav } from '@yanfan/bmpch-tsapi/web-api';
import { usePermission } from '@yanfan/common/hooks';

defineOptions({
  name: 'BmpchWebApiEdit',
  titl: 'Wep接口编辑',
});

const { hasPermission } = usePermission();

const props = withDefaults(
  defineProps<{
    show: boolean;
    width?: number;
    bizId: string | null;
  }>(),
  {
    show: false,
    width: 530,
  },
);

const emits = defineEmits<{
  (e: 'update:show', show: boolean): void;
  (e: 'refresh'): void;
}>();

class FormData {
  apiId: string = '';
  apiNm: string = '';
  path: string = '';
  methd: string = '';
  reqTotl: number = 0;
  reqMaxDur: number = 0;
  grpId: string = '';
  appCd: string = '';
  chnlId: string = '';
  sysId: string = '';
  rmrk: string = '';
}

const formRules: FormRules = {
  path: {
    required: true,
    trigger: ['blur', 'input'],
    message: '请输入网络地址',
  },
  methd: {
    required: true,
    trigger: ['blur', 'input'],
    message: '请输入方法',
  },
  reqTotl: {
    required: true,
    trigger: ['blur', 'input'],
    message: '请输入请求总数',
  },
  reqMaxDur: {
    required: true,
    trigger: ['blur', 'input'],
    message: '请输入请求最大耗时',
  },
  chnlId: {
    required: true,
    trigger: ['blur', 'input'],
    message: '请输入渠道编号',
  },
};

const title = ref('新建');
const modalLoading = ref(false);
const formRef = ref<FormInst | null>(null);
const showModal = ref(false);
const saveBtnLoading = ref(false);
const formData = reactive(new FormData());

watch(
  () => unref(props).show,
  (show) => {
    showModal.value = show;
    if (showModal.value) {
      if (props.bizId) {
        title.value = '编辑';
        modalLoading.value = true;
        webApiGet({ apiId: props.bizId })
          .then((data: BMPCH.WebApiGetOutput) => {
            Object.assign(formData, data);
          })
          .finally(() => {
            modalLoading.value = false;
          });
      } else {
        title.value = '新建';
        Object.assign(formData, new FormData());
      }
    } else {
      Object.assign(formData, new FormData());
    }
  },
);

function handleUpdateShow(value: boolean) {
  emits('update:show', value);
}

function handleSave() {
  saveBtnLoading.value = true;
  formRef.value
    ?.validate()
    .then(() => {
      webApiSav(formData).then((data: BMPCH.WebApiSavOutput) => {
        if (data.success) {
          formData.apiId = data.apiId;
          handleUpdateShow(false);
          window.$message.success('保存成功');
          emits('refresh');
        } else {
          window.$message.error(data.errmsg);
        }
      });
    })
    .catch(() => {
      window.$message.error('请填写完整信息');
    })
    .finally(() => {
      saveBtnLoading.value = false;
    });
}
</script>

<style lang="scss" scoped></style>
